﻿@model IEnumerable<Category>
@using SIG.Model.Enums;
@using SIG.Infrastructure.UI

@{
    ViewBag.PageTitle = ViewBag.ModelTypeName + "分类";
    ViewBag.Title = ViewBag.PageTitle + "_内容";
    Layout = "~/Areas/Admin/Views/Shared/_Layout.cshtml";
}


<div class="main-content">


    <div class="panel panel-default">
        <header class="panel-heading">
            <h3 class="panel-title"><span class="glyphicon glyphicon-th-large"></span> @ViewBag.PageTitle </h3>
        </header>

        <section class="panel-body">
            <div class="text-right">
                @Ajax.RawActionLink("<i class='fa fa-plus fa-fw'></i>添加分类",
                        "CreateCategory", "Product",
                        new { },
                        new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "edit-container", OnComplete = "onAjaxComplete", OnSuccess = "OnSuccessLink" },
                        new { @class = "btn btn-primary pull-right" })

            </div>
        </section>


        <table class="table table-hover table-vmiddle">
            <thead>
                <tr>
                    <th>

                    </th>
                    <th>
                        主题
                    </th>

                    <th>
                        排序
                    </th>

                    <th>
                        显示
                    </th>
                    <th>
                        创建日期
                    </th>
                    <th>
                        创建者
                    </th>
                </tr>
            </thead>
            <tbody>
                @foreach (var category in Model)
                {
                    var className = category.Active ? "glyphicon-eye-open" : "glyphicon-eye-close";
                    var actionName = category.Active ? "隐藏" : "显示";
                    <tr class="first">
                        <td>
                            <!-- Split button -->
                            <div class="btn-group">
                                <button type="button" class="btn btn-primary">操作</button>
                                <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                    <span class="caret"></span>
                                    <span class="sr-only">Toggle Dropdown</span>
                                </button>
                                <ul class="dropdown-menu ">

                                    <li>
                                        @Ajax.RawActionLink("<i class='fa fa-edit fa-fw'></i>编辑",
                                        "EditCategory", "Product",
                                        new { id = category.Id },
                                        new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "edit-container", OnComplete = "onAjaxComplete", OnSuccess = "OnSuccessLink" },
                                        new { })
                                    </li>
                                    <li>

                                        <a href="#" data-url="@Url.Action("ActiveCategory", new {id = category.Id})" class="active-item">
                                            @Html.Raw(category.Active ? "<i class='fa fa-eye-slash fa-fw'></i>隐藏" : "<i class='fa fa-eye fa-fw'></i>显示")
                                        </a>
                                    </li>
                                    <li role="separator" class="divider"></li>
                                    <li>
                                        <a href="#" data-url="@Url.Action("DeleteCategory", new {id = category.Id})" class="delete-item" title="删除">
                                            <i class="fa fa-trash-o fa-fw"></i>删除
                                        </a>
                                    </li>
                                </ul>
                            </div>

                        </td>
                        <td>
                            @category.Title
                        </td>
                        <td>
                            @category.Importance
                        </td>
                        <td>
                            <input type="checkbox" disabled @(category.Active ? "Checked" : "") />
                        </td>
                        <td>@category.CreatedDate.ToShortDateString()</td>
                       
                        <td>
                            @category.CreatedBy
                        </td>
                    </tr>

                    if (category.ChildCategories.Any())
                    {
                        foreach (var childCategory in category.ChildCategories)
                        {
                            var classNameChild = childCategory.Active ? "glyphicon-eye-open" : "glyphicon-eye-close";
                            var actionNameChild = childCategory.Active ? "隐藏" : "显示";

                            <tr class="first">
                                <td>
                                    <!-- Split button -->
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-primary">操作</button>
                                        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                                            <span class="caret"></span>
                                            <span class="sr-only">Toggle Dropdown</span>
                                        </button>
                                        <ul class="dropdown-menu ">

                                            <li>
                                                @Ajax.RawActionLink("<i class='fa fa-edit fa-fw'></i>编辑",
                                                "EditCategory", "Product",
                                                new { id = childCategory.Id },
                                                new AjaxOptions { HttpMethod = "Get", UpdateTargetId = "edit-container", OnComplete = "onAjaxComplete", OnSuccess = "OnSuccessLink" },
                                                new { })
                                            </li>
                                            <li>

                                                <a href="#" data-url="@Url.Action("ActiveCategory", new {id = childCategory.Id})" class="active-item">
                                                    @Html.Raw(category.Active ? "<i class='fa fa-eye-slash fa-fw'></i>隐藏" : "<i class='fa fa-eye fa-fw'></i>显示")
                                                </a>
                                            </li>
                                            <li role="separator" class="divider"></li>
                                            <li>
                                                <a href="#" data-url="@Url.Action("DeleteCategory", new {id = childCategory.Id})" class="delete-item" title="删除">
                                                    <i class="fa fa-trash-o fa-fw"></i>删除
                                                </a>
                                            </li>
                                        </ul>
                                    </div>
                                </td>
                                <td>
                                    <span class="fa fa-long-arrow-right"></span>
                                    @childCategory.Title
                                </td>
                                <td>
                                    @childCategory.Importance
                                </td>
                                <td>
                                    <input type="checkbox" disabled @(category.Active ? "Checked" : "") />
                                </td>
                                <td>@childCategory.CreatedDate.ToShortDateString()</td>
                                <td>
                                    @category.CreatedBy
                                </td>
                            </tr>
                        }
                    }
                }

            </tbody>

        </table>



        <footer class="panel-footer">
            @*<ul class="pagination">
                    @Html.PageLinks(Model.PagingInfo, x => Url.Action("Categories", new { Page = x }))
                </ul>*@
        </footer>


    </div>
</div>
<!-- end main container -->

<div id="edit-container">
</div>

<form id="anti-form" method="post">
    @Html.AntiForgeryToken()
</form>


@section footer {
    @Scripts.Render("~/bundles/jqueryvalWithBootstrap")

    <script>

        function OnSuccessLink(data) {
            Common.ShowBox(data.Status, data.Message, "操作")
        }

        function onAjaxComplete() {
            $('#ModalEdit').modal();
        }
        function onBegin() {
            $("#btnSave i").removeClass("fa-save").addClass("fa-spinner fa-spin")
        }
        function onComplete() {
            $("#btnSave i").removeClass("fa-spinner fa-spin").addClass("fa-save")
        }


        function updateSuccess(data) {

            switch (data.Status) {
                case 2:
                    toastr.error(data.Message, '@ViewBag.Title');
                    break;
                case 1:
                    toastr.success(data.Message, '@ViewBag.Title');
                    $('#ModalEdit').modal('hide');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                    break;
                case 4:
                    toastr.warning(data.Message, "@ViewBag.Title")
                    break;
            }

        }


        $(function () {


            //active
            $(document).on("click", '.active-item', function (e) {
                e.preventDefault();
                var url = $(this).attr('data-url');

                $.post(url, $("#anti-form").serialize(), function (data) {
                    Common.ShowBox(data.Status, data.Message, "操作")

                    setTimeout(function () {
                        location.reload();
                    }, 1000);


                });


            });

            //delete
            $(document).on("click", '.delete-item', function (e) {
                e.preventDefault();
                var url = $(this).attr('data-url');

                bootbox.setLocale("zh_CN");

                bootbox.confirm("是否确定删除该项？", function (result) {
                    if (result) {
                        $.post(url, $("#anti-form").serialize(), function (data) {

                            Common.ShowBox(data.Status, data.Message, "操作")

                            setTimeout(function () {
                                location.reload();
                            }, 1000);


                        });
                    }
                });
            });


        });
    </script>

}